package com.xuebank.nfe.nfecar.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.licrafter.tagview.DIRECTION;
import com.xuebank.nfe.nfecar.R;
import com.xuebank.nfe.nfecar.model.TagGroupModel;
import com.xuebank.nfe.nfecar.utils.DirectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: shell
 * date 2017/1/4 上午10:46
 **/
public class TagEditDialog extends Dialog implements View.OnClickListener {

    private EditText tag01, tag02, tag03;
    private Button cancel, confirm;
    private OnTagEditDialogClickListener listener;

    public TagEditDialog(@NonNull Context context, OnTagEditDialogClickListener listener) {
        super(context);
        setContentView(R.layout.dialog_tag_edit);

        //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
        //一定要在setContentView之后调用，否则无效
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        this.listener = listener;
        tag01 = (EditText) findViewById(R.id.tag01);
//        tag02 = (EditText) findViewById(R.id.tag02);
//        tag03 = (EditText) findViewById(R.id.tag03);
        cancel = (Button) findViewById(R.id.cancelBtn);
        confirm = (Button) findViewById(R.id.confirmBtn);
        cancel.setOnClickListener(this);
        confirm.setOnClickListener(this);
    }

    public TagGroupModel createTagGroup() {
        TagGroupModel model = new TagGroupModel();
        List<TagGroupModel.Tag> tagList = new ArrayList<>();
        String str01 = tag01.getText().toString();

//        String fangxiang = DIRECTION.LEFT_CENTER;
//        String str02 = tag02.getText().toString();
//        String str03 = tag03.getText().toString();
        TagGroupModel.Tag tag = new TagGroupModel.Tag();
        if (!TextUtils.isEmpty(str01)) {

            tag.setName(str01);
        }



        model.getTags().add(tag);

        setTagDirection(tag);
        model.setPercentX(0.5f);
        model.setPercentY(0.1334530f);
        return model;
    }

    private void setTagDirection(TagGroupModel.Tag tagList) {

        tagList.setDirection(DirectionUtils.getValue(DIRECTION.RIGHT_CENTER));

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancelBtn:
                listener.onCancel();
                break;
            case R.id.confirmBtn:
                listener.onTagGroupCreated(createTagGroup());
                break;
        }
    }

    public interface OnTagEditDialogClickListener {
        void onCancel();

        void onTagGroupCreated(TagGroupModel group);
    }
}
